package com.jarchie.htgl.activity;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.TextInputLayout;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import com.coder.zzq.smartshow.toast.SmartToast;
import com.jarchie.htgl.base.BaseActivity;
import com.jarchie.htgl.base.BaseApp;
import com.jarchie.htgl.R;
import com.jarchie.htgl.bean.UserLogin;
import com.jarchie.htgl.constants.ConfigConstant;
import com.jarchie.htgl.network.Api;
import com.jarchie.htgl.utils.CommonUtils;
import com.jarchie.htgl.utils.LogUtil;
import com.jarchie.htgl.views.DialogLoader;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Jarchie on 2017\12\11.
 * 用户登录的Activity
 */

public class LoginActivity extends BaseActivity implements View.OnClickListener {
    private LinearLayout containLayout;
    private TextInputLayout userNameLayout, passWordLayout;
    private EditText editUName, editPwd;
    private Button btnLogin;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        containLayout = obtainView(R.id.id_login_layout);
        userNameLayout = obtainView(R.id.layout_username);
        passWordLayout = obtainView(R.id.layout_password);
        editUName = obtainView(R.id.edit_username);
        editPwd = obtainView(R.id.edit_password);
        btnLogin = obtainView(R.id.btn_login);
    }

    @Override
    protected void setListener() {
        btnLogin.setOnClickListener(this);
        CommonUtils.addLayoutListener(containLayout, btnLogin); //键盘监听
    }

    @Override
    protected void initData() {
        userNameLayout.setHint(getString(R.string.user_name));
        passWordLayout.setHint(getString(R.string.user_password));
        getSpInfo(); //读取配置文件中保存的用户名和密码
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                if (TextUtils.isEmpty(editUName.getText().toString()) || TextUtils.isEmpty(editPwd.getText().toString())) {
                    SmartToast.showInCenter("请填写完整用户名和密码");
                } else {
                    //调用网络请求
                    DialogLoader.show(this);
                    Api.getInstance()
                            .requestLogin(editUName.getText().toString(), editPwd.getText().toString())
                            .enqueue(new Callback<UserLogin>() {
                                @Override
                                public void onResponse(@NonNull Call<UserLogin> call, @NonNull Response<UserLogin> response) {
                                    DialogLoader.dismiss();
                                    if (response.isSuccessful()) {
                                        if (response.body().getMSG().equals(ConfigConstant.SUCCESS)){
                                            UserLogin userLogin = response.body();
                                            LogUtil.e(userLogin.getINFO().get(0).getXm());
                                            //保存用户登录信息到Application中去，这样可以全局获取内部信息
                                            BaseApp.getInstance().setUserLogin(userLogin);
                                            //保存用户名和密码到配置文件中去
                                            BaseApp.getSpUtils().put(ConfigConstant.SHARE_UNAME_KEY, editUName.getText().toString());
                                            BaseApp.getSpUtils().put(ConfigConstant.SHARE_PWD_KEY, editPwd.getText().toString());
                                            SmartToast.showInCenter("登录成功");
                                            startActivity(new Intent(LoginActivity.this, MainActivity.class));
                                            finish();
                                        }else if (response.body().getMSG().equals(ConfigConstant.ERROR)){
                                            SmartToast.showInCenter(getString(R.string.tips_error_data));
                                        }
                                    }
                                }

                                @Override
                                public void onFailure(@NonNull Call<UserLogin> call, @NonNull Throwable t) {
                                    SmartToast.showInCenter(getString(R.string.tips_failure_data));
                                    DialogLoader.dismiss();
                                }
                            });
                }
                break;
        }
    }

    //从配置文件中获取用户名和密码，若不为空则直接填入上一次保存的用户名和密码
    private void getSpInfo() {
        String uNameStr = BaseApp.getSpUtils().getString(ConfigConstant.SHARE_UNAME_KEY);
        String pwdStr = BaseApp.getSpUtils().getString(ConfigConstant.SHARE_PWD_KEY);
        if (uNameStr != null) {
            editUName.setText(uNameStr);
        }
        if (pwdStr != null) {
            editPwd.setText(pwdStr);
        }
    }

    //如果用户此时不登录，直接点击返回键，这里彻底退出应用程序，将进程杀死
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            this.finish();
            CommonUtils.killApp();
        }
        return super.dispatchKeyEvent(event);
    }

}